import api from '@/pages/measurements/api'
import { getData } from '@/servers/http'
import { Toast } from 'antd-mobile-v2'
import { useEffect, useState } from 'react'
import { history } from 'umi'

export default function (id) {
    // 分享弹窗可视状态
    const [shareModalVisible, setShareModalVisible] = useState(false)
    // 说明弹窗可视状态
    const [modalVisible, setModalVisible] = useState(false)
    // 测评详情
    const [detail, setDetail] = useState(null)
    // 测评结果ID
    const [resultId, setResultId] = useState(null)

    useEffect(() => {
        getEvalDetail(id)
    }, [])

    const getEvalDetail = async id => {
        const res = (await getData(`${api.testEvals}${id}?time=${new Date().valueOf()}`)) || {}
        const { status, data } = res || {}
        if (status !== 200) return
        setDetail(data || {})
        data?.paper?.id && setResultId(data.paper.id)
    }

    // 去测评
    const toExamPage = () => {
        console.log('详情页的立即测试')
        if (detail?.publish_status === '0') {
            Toast.fail('该测评已下架！')
            return
        }

        history.push(`/measurements/exam?id=${id}`)
    }

    // 查看结果
    const toResult = () => {
        history.push(`/measurements/result?eval_id=${id}&result_id=${resultId}`)
    }

    const startTest = () => {
        // // 有测评说明点击展示说明弹窗
        // detail?.eval_explain && !modalVisible && setModalVisible(true)
        // // 有测试说明且弹窗已展示点击开始测评
        // detail?.eval_explain && modalVisible && toExamPage()
        // // 无测评说明直接开始测评
        // !detail?.eval_explain && toExamPage()

        // 点击立即测试按钮 有resultId 说明当前用户已经进行过一次测试
        // console.log('id===>', resultId)
        toExamPage()
    }

    const hideModal = () => {
        setModalVisible(false)
    }

    return {
        detail,
        resultId,
        modalVisible,
        shareModalVisible,
        setShareModalVisible,
        hideModal,
        startTest,
        toResult,
    }
}
